<?php
class M_post extends Module {
  public function main() {
    if ( !Site::$ajax ) {
      // usual output
      $id = isset( Site::$URL[2] )  ?  (int) Site::$URL[2] : 0; //get id
      // post saving/editing
      if ( isset($_POST['title']) || isset($_POST['text']) ) {
        $_POST['id'] = $id;
        $_POST['type'] = '';
        $_POST['permalink'] = translit($_POST['title']);
        $post = publication_construct( $_POST );
        $post->save();
        $post->tags( $_POST['tags'] );
      }

      $post = publication_construct( $id );
      $tmp = new Template( 'pan_post' );
      $tmp->assign( 'post', $post );
      return $tmp;
      
    } else {
      switch ( $_GET['act'] ) {
        case 'del':
          if ( !isset( $_GET['id'] ) ) {
            continue;
          }
          $id = $_GET['id']+0;
          $sql = "
         DELETE FROM `posts` WHERE `id` = ':1' ;;
         DELETE FROM `tags_relat` WHERE `post`=':1'";
          if ( db::q( $sql, $id )->ready() ) {
            $out = 'OK';
          } else {
            $out = 'ERROR';
          }
          break;
        default:
          echo 'Parameters missing';
          break;
      }
    }
  
  }
}

?>